/**
 * Project: lionbule-other
 * 
 * File Created at 2012-4-2
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.poi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

/**
 * BuildProperty.
 * 
 * @author LionBule 2012-4-2 上午10:20:40
 */
public class BuildProperty {
    private String[]             titles        = new String[] { "FieldName", "CodeName",
            "DataType", "ValidValues", "FieldsDiscription", "更新频率", "数据时效", "是否已接入A_GDS" };

    //1:Long, 2:Double, 3:String, 4:Boolean, 5:Calander, 6:Integer
    private Map<String, Integer> typeMapping   = new HashMap<String, Integer>();

    private Map<String, Integer> statusMapping = new HashMap<String, Integer>();

    {
        //status
        statusMapping.put("未提交", 1);
        statusMapping.put("接入中", 2);
        statusMapping.put("已接入", 3);
        statusMapping.put("已下线", 4);

        //type
        typeMapping.put("Long", 1);
        typeMapping.put("Double", 2);
        typeMapping.put("String", 3);
        typeMapping.put("Boolean", 4);
        typeMapping.put("Calendar", 5);
        typeMapping.put("Integer", 6);
        typeMapping.put("Calandar", 5);
        typeMapping.put("Calander", 5);
    }

    private Set<String>          titleSet      = new HashSet<String>();
    private List<MapDO>          titleList     = new ArrayList<BuildProperty.MapDO>();
    private Sheet                sheet;

    public Sheet getSheet() {
        return sheet;
    }

    public BuildProperty(Sheet sheet) {
        this.sheet = sheet;
        init(sheet);
    }

    private void init(Sheet sheet) {
        //System.out.println("----- sheet name = " + sheet.getSheetName());

        for (String s : titles) {
            titleSet.add(s);
        }

        Row title = sheet.getRow(1);
        if (title != null) {
            for (Cell cell : title) {
                int index = cell.getColumnIndex();
                String name = cell.getStringCellValue().trim().replaceAll(" ", "");
                if (titleSet.contains(name)) {
                    MapDO mapDO = new MapDO(index, name);
                    titleList.add(mapDO);
                }
            }
        }

    }

    public List<PropertyDO> getAllProperty() {
        String lineSep = System.getProperty("line.separator");

        List<PropertyDO> rs = new ArrayList<PropertyDO>();

        for (Row row : sheet) {
            int rownum = row.getRowNum();
            if (rownum < 2)
                continue;

            PropertyDO pdo = new PropertyDO();
            for (MapDO m : titleList) {
                int cellnum = m.index;
                Cell cell = row.getCell(cellnum);
                if (cell != null) {
                    String value = cell.getStringCellValue();
                    if (value != null) {
                        value = value.replaceAll("\\n", "");
                    }
                    setup(m.name, pdo, value);
                }
            }
            rs.add(pdo);
        }

        return rs;
    }

    private void setup(String name, PropertyDO pdo, String value) {
        if (name.equals("FieldName")) {
            pdo.setName(value);
        } else if (name.equals("CodeName")) {
            pdo.setCode(value);
        } else if (name.equals("DataType")) {
            Integer i = typeMapping.get(value);
            pdo.setType(i == null ? 0 : i);
        } else if (name.equals("ValidValues")) {
            pdo.setValidValues(value);
        } else if (name.equals("FieldsDiscription")) {
            pdo.setDesc(value);
        } else if (name.equals("更新频率")) {
            pdo.setFrequency(value);
        } else if (name.equals("数据时效")) {
            pdo.setAging(value);
        } else if (name.equals("是否已接入A_GDS")) {
            Integer i = statusMapping.get(value);
            pdo.setStatus(i == null ? 1 : i);
        }
    }

    class MapDO {
        int    index;
        String name;

        public MapDO(int index, String name) {
            this.index = index;
            this.name = name;
        }
    }

}
